using System.Windows.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Keyboard = Microsoft.Xna.Framework.Input.Keyboard;

namespace Silverlight3dApp.Shaders.Lesson02
{
    public class MadnessGame : Game
    {
        private Camera _camera;

        private VertexBuffer _vertexBuffer;
        private VertexPositionColor[] _vertexPositionColors;
        private VertexPositionNormalColor[] _vertexPositionNormalColors;

        private SilverlightEffect _effect;
        private BasicEffect _basicEffect;

        private Matrix _worldTranslation = Matrix.Identity;
        private Matrix _worldRotation = Matrix.Identity;

        protected override void Initialize()
        {
            _camera = new Camera(this, new Vector3(0, 0, 5), new Vector3(0, 0, 0), Vector3.Up);
            Components.Add(_camera);
            base.Initialize();
        }

        protected override void LoadContent()
        {
            _vertexPositionColors = new VertexPositionColor[4];
            _vertexPositionColors[0] = new VertexPositionColor(new Vector3(-1, 1, 0), Colors.Gray);
            _vertexPositionColors[1] = new VertexPositionColor(new Vector3(1, 1, 0), Colors.Gray);
            _vertexPositionColors[2] = new VertexPositionColor(new Vector3(-1, -1, 0), Colors.Gray);
            _vertexPositionColors[3] = new VertexPositionColor(new Vector3(1, -1, 0), Colors.Gray);

            _vertexBuffer = new VertexBuffer(GraphicsDevice, VertexPositionColor.VertexDeclaration, _vertexPositionColors.Length, BufferUsage.WriteOnly);
            _vertexBuffer.SetData(_vertexPositionColors);

            //_vertexPositionNormalColors = new VertexPositionNormalColor[4];
            //_vertexPositionNormalColors[0] = new VertexPositionNormalColor(new Vector3(-1, 1, 0), Vector3.UnitZ, Colors.Blue);
            //_vertexPositionNormalColors[1] = new VertexPositionNormalColor(new Vector3(1, 1, 0), Vector3.UnitZ, Colors.Purple);
            //_vertexPositionNormalColors[2] = new VertexPositionNormalColor(new Vector3(-1, -1, 0), Vector3.UnitZ, Colors.Green);
            //_vertexPositionNormalColors[3] = new VertexPositionNormalColor(new Vector3(1, -1, 0), Vector3.UnitZ, Colors.Red);

            //_vertexBuffer = new VertexBuffer(GraphicsDevice, VertexPositionNormalColor.VertexDeclaration, _vertexPositionNormalColors.Length, BufferUsage.WriteOnly);
            //_vertexBuffer.SetData(_vertexPositionNormalColors);

            //_basicEffect = new BasicEffect(GraphicsDevice);
            _effect = Content.Load<SilverlightEffect>(@"Effects/DiffuseLight");

            base.LoadContent();
        }

        protected override void Update(GameTime gameTime)
        {
            var keyState = Keyboard.GetState();
            if (keyState.IsKeyDown(Key.Left))
            {
                _worldTranslation *= Matrix.CreateTranslation(-0.01f, 0, 0);
            }
            if (keyState.IsKeyDown(Key.Right))
            {
                _worldTranslation *= Matrix.CreateTranslation(0.01f, 0, 0);
            }
            _worldRotation *= Matrix.CreateRotationY(MathHelper.PiOver4 / 120);
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            if (GraphicsDevice.RasterizerState.CullMode != CullMode.None)
            {
                GraphicsDevice.RasterizerState = new RasterizerState
                                                     {
                                                         CullMode = CullMode.None
                                                     };
            }
            //GraphicsDevice.Clear(ClearOptions.Target | ClearOptions.DepthBuffer, Colors.CornflowerBlue, 1.0f, 0);
            GraphicsDevice.SetVertexBuffer(_vertexBuffer);

            //_basicEffect.World = _worldRotation * _worldTranslation;
            //_basicEffect.View = _camera.View;
            //_basicEffect.Projection = _camera.Projection;
            //_basicEffect.VertexColorEnabled = true;
            //_basicEffect.EnableDefaultLighting();
            //_basicEffect.AmbientLightColor = Colors.Yellow.ToVector3();
            //_basicEffect.DiffuseColor = Colors.LightYellow.ToVector3();
            //_basicEffect.DirectionalLight0.Enabled = true;
            //foreach (var pass in _basicEffect.CurrentTechnique.Passes)
            //{
            //    pass.Apply();
            //    GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
            //}

            var worldViewProjection = _worldRotation * _worldTranslation * _camera.View * _camera.Projection;
            _effect.Parameters["WorldViewProjection"].SetValue(worldViewProjection);
            _effect.Parameters["InvertedWorldViewProjection"].SetValue(Matrix.Invert(worldViewProjection));
            _effect.Parameters["LightDirection"].SetValue(new Vector4(0, -1, 1, 1));
            _effect.CurrentTechnique = _effect.Techniques[1];
            foreach (var pass in _effect.CurrentTechnique.Passes)
            {
                pass.Apply();
                GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
            }
            base.Draw(gameTime);
        }
    }
}